package com.hl.card.modular.packBack.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.persistence.dao.BillMapper;
import com.hl.card.common.persistence.dao.CommunityUserMapper;
import com.hl.card.common.persistence.model.Bill;
import com.hl.card.common.persistence.model.CommunityUser;
import com.hl.card.common.util.DateUtils;
import com.hl.card.modular.packBack.service.BillService;

/**
 * 账单业务逻辑
 * 
 * @author 袁伟
 *
 */
@Service
public class BillServiceImpl implements BillService {
	@Autowired
	private BillMapper billMapper;
	@Autowired
	private CommunityUserMapper  communityUserMapper;
    @Value("${auth_count}")
    private Integer auth_count;
	/**
	 * 出账
	 */
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void add() {
		Map<String,Object> param=new HashMap<String, Object>();
		param.put("auth_count",auth_count);
		String  max_no=billMapper.findMaxNo();
		int index=1;
		if(!StringUtils.isEmpty(max_no))
		{
			String no=max_no.split("-")[1];
			index=Integer.parseInt(no)+1;
		}
		//查询需要新增的账单
		List<CommunityUser> list=communityUserMapper.findNeedAddBill(param);
		for(int i=0;i<list.size();i++)
		{
			CommunityUser communityUser=list.get(i);
			String format_num = String.format("%04d", index);  
			String bill_no=getNo(format_num);
			Bill bill=new Bill();
			bill.setBill_no(bill_no);  //设置账单编号
			bill.setCommunity_code(communityUser.getCommunity_code());
			bill.setAccount(communityUser.getAccount());
			bill.setNum(communityUser.getAuth_count());
			bill.setStatus(0);
			bill.setTime(DateUtils.getNow());
			bill.setUser_id(communityUser.getUser_id());
			billMapper.add(bill);  //新增账单
			index++;
		}
		//更新群主表认证数量
		communityUserMapper.updateBillCount(param);
	}

	/**
	 * 查询列表
	 */
	@Override
	public List<Bill> list(Page<Bill> page, Bill bill, String sort, String order) {
		int skip = (page.getCurrent() - 1) * page.getSize();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("offset", skip);
		map.put("size", page.getSize());
		if (!StringUtils.isEmpty(sort)) {
			map.put("orderFiled", "order by " + sort + " " + order);
		}
		if (!StringUtils.isEmpty(bill.getAccount())) {
			map.put("account", bill.getAccount());
		}
		if (!StringUtils.isEmpty(bill.getCommunity_code())) {
			map.put("community_code", bill.getCommunity_code());
		}
		if (bill.getStatus() != null) {
			map.put("status", bill.getStatus());
		}
		if(!StringUtils.isEmpty(bill.getStart_time())&&!StringUtils.isEmpty(bill.getEnd_time()))
		{
			map.put("start_time", bill.getStart_time()+" 00:00:00");
			map.put("end_time", bill.getEnd_time()+" 59:59:59");
		}
		page.setTotal(billMapper.listCount(map));
		return billMapper.list(map);
	}

	@Override
	public List<Map<String, Object>> exportExcel(Bill bill) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(bill.getAccount())) {
			map.put("account", bill.getAccount());
		}
		if (!StringUtils.isEmpty(bill.getCommunity_code())) {
			map.put("community_code", bill.getCommunity_code());
		}
		if (bill.getStatus() != null) {
			map.put("status", bill.getStatus());
		}
		if(!StringUtils.isEmpty(bill.getStart_time())&&!StringUtils.isEmpty(bill.getEnd_time()))
		{
			map.put("start_time", bill.getStart_time()+" 00:00:00");
			map.put("end_time", bill.getEnd_time()+" 59:59:59");
		}
		return billMapper.exportExcel(map);
	}
	
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
	public void switchStatus(Integer status, Integer id) {
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("status", status);
		map.put("id", id);
		if(status==0)
		{
			map.put("settle_time", null);
		}else
		{
			map.put("settle_time", DateUtils.getNow());
		}
		billMapper.switchStatus(map);
		
	}
	
	/**
	 * 生成编号
	 * @return
	 */
	public String getNo(String format_num)
	{
		return "RZ"+DateUtils.getNow("yyyyMMdd")+"-"+format_num;
	}

	@Override
	public int findBillCount(Map<String, Object> map) {
		return communityUserMapper.findBillCount(map);
	}
}
